/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package wekimini.gui;

import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import wekimini.InputManager;
import wekimini.InputManager.InputListener;
import wekimini.Wekinator;
import wekimini.util.Util;

/**
 *
 * @author rebecca
 */
public class GUIInputRow extends javax.swing.JPanel {

    private final Wekinator w;
    private final InputManager inputManager;
    private final ImageIcon onIcon = new ImageIcon(getClass().getResource("/mockups/icons/greenlight2.png")); // NOI18N
    private final ImageIcon offIcon = new ImageIcon(getClass().getResource("/mockups/icons/greylight2.png")); // NOI18N
    private  String name;
    private final InputListener inputListener;
    ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(1);
    ScheduledFuture scheduledFuture;
    
    /**
     * Creates new form GUIInputRow
     */
    public GUIInputRow(Wekinator w, String name) {
        initComponents();
        this.w = w;
        inputManager = w.getInputManager();
        //checkEnabled.setEnabled(inputManager.getEnabled(name));
        labelName.setText(name);
        this.name = name;
        inputListener = new InputListener() {

            @Override
            public void update(double[] vals) {
                updateLight();
            }

            @Override
            public void notifyInputError() {
            }

            @Override
            public void updateBundle(List<List<Double>> values) {
                updateLight();
            }
        };
        
        inputManager.addInputValueListener(inputListener);
        //inputManager.addInputValueListener(name, 
        
        scheduledFuture = scheduledExecutorService.scheduleWithFixedDelay(new Runnable() {

            @Override
            public void run() {
                labelIndicator.setIcon(offIcon);
            }
        }, 1, 1, TimeUnit.SECONDS);
    }

    private void updateLight() {
        //TODO: turn off; check repaint; check other thread!
        labelIndicator.setIcon(onIcon);
        repaint();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        labelName = new javax.swing.JLabel();
        labelIndicator = new javax.swing.JLabel();
        buttonLook = new javax.swing.JButton();
        buttonEdit = new javax.swing.JButton();

        setMaximumSize(new java.awt.Dimension(32767, 25));

        labelName.setText("HID Inputs");

        labelIndicator.setIcon(new javax.swing.ImageIcon(getClass().getResource("/wekimini/icons/greylight2.png"))); // NOI18N

        buttonLook.setIcon(new javax.swing.ImageIcon(getClass().getResource("/wekimini/icons/mag3.png"))); // NOI18N
        buttonLook.setMaximumSize(new java.awt.Dimension(35, 35));
        buttonLook.setMinimumSize(new java.awt.Dimension(35, 35));
        buttonLook.setPreferredSize(new java.awt.Dimension(35, 35));
        buttonLook.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonLookActionPerformed(evt);
            }
        });

        buttonEdit.setIcon(new javax.swing.ImageIcon(getClass().getResource("/wekimini/icons/pencil1.png"))); // NOI18N
        buttonEdit.setMaximumSize(new java.awt.Dimension(35, 35));
        buttonEdit.setPreferredSize(new java.awt.Dimension(35, 35));
        buttonEdit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonEditActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(labelName, javax.swing.GroupLayout.PREFERRED_SIZE, 206, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 53, Short.MAX_VALUE)
                .addComponent(labelIndicator, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(buttonLook, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(buttonEdit, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(labelName, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(labelIndicator, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                .addComponent(buttonEdit, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                .addComponent(buttonLook, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void buttonEditActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonEditActionPerformed
       // TODO: w.getMainGUI().displayEditInput();
    }//GEN-LAST:event_buttonEditActionPerformed

    private void buttonLookActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonLookActionPerformed
      
       // TODO: w.getInputManager().viewInputs();
    }//GEN-LAST:event_buttonLookActionPerformed

    //TODO: Make sure this gets called any time input viewer goes out of scope!
    //e.g. after project builder is completed
    public void prepareToDie() {
        inputManager.removeInputValueListener(inputListener);
    }
    

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton buttonEdit;
    private javax.swing.JButton buttonLook;
    private javax.swing.JLabel labelIndicator;
    private javax.swing.JLabel labelName;
    // End of variables declaration//GEN-END:variables
}
